package cn.iocoder.yudao.module.famous.doctor.controller.app.forum;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.forum.vo.*;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.reply.Enum.ReplyEnum;
import cn.iocoder.yudao.module.famous.doctor.convert.forum.FamForumConvert;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.forum.FamForumDO;
import cn.iocoder.yudao.module.famous.doctor.service.forum.FamForumService;
import cn.iocoder.yudao.module.famous.doctor.service.forum.FamForumServiceImpl;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.vo.MemberUserParamDO;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 APP - 论坛表")
@RestController
@RequestMapping("/famous.doctor/fam-forum/app")
@Validated
public class AppFamForumController {

    @Resource
    private FamForumService famForumService;
    @Resource
    private MemberUserService memberUserService;
    @Resource
    private FamForumServiceImpl forumService;
    @PostMapping("/create")
    @Operation(summary = "创建论坛表")
    public CommonResult<Long> createFamForum(@Valid @RequestBody FamForumCreateReqVO createReqVO) {
        return success(famForumService.createFamForum(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新论坛表")
    public CommonResult<Boolean> updateFamForum(@Valid @RequestBody FamForumUpdateReqVO updateReqVO) {
        famForumService.updateFamForum(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除论坛表")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteFamForum(@RequestParam("id") Long id) {
        famForumService.deleteFamForum(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得论坛表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<FamForumRespVO> getFamForum(@RequestParam("id") Long id) {
        FamForumDO famForum = famForumService.getFamForum(id);
        String creator = famForum.getCreator();
        FamForumRespVO convert = FamForumConvert.INSTANCE.convert(famForum);
        MemberUserDO user = memberUserService.getUser(Long.valueOf(creator));
        if (user!=null) {
            MemberUserParamDO memberUserParamDO = new MemberUserParamDO();
            BeanUtils.copyProperties(user,memberUserParamDO);
            convert.setPublishUserMessage(memberUserParamDO);
        }
        //查询谈论点赞数量
        convert.setUpvoteCount(forumService.getCollectCount(id, ReplyEnum.REPLY_UPVOTE));
        return success(convert);
    }

    @GetMapping("/list")
    @Operation(summary = "获得论坛表列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    public CommonResult<List<FamForumRespVO>> getFamForumList(@RequestParam("ids") Collection<Long> ids) {
        List<FamForumDO> list = famForumService.getFamForumList(ids);
        return success(FamForumConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得论坛表分页")
    public CommonResult<PageResult<FamForumRespVO>> getFamForumPage(@Valid FamForumPageReqVO pageVO) {
        PageResult<FamForumDO> pageResult = famForumService.getFamForumPage(pageVO);
        return success(FamForumConvert.INSTANCE.convertPage(pageResult));
    }

    @PostMapping("/pageParticulars")
    @Operation(summary = "获得论坛详细信息")
    public CommonResult<PageResult<FamForumRespTwoVO>> getFamForumPageParticulars(@Valid @RequestBody FamForumPageReqVO pageVO) {
        PageResult<FamForumRespTwoVO> pageResult = famForumService.getFamForumPageParticulars(pageVO);
        return success(pageResult);
    }

    @PostMapping("/randomParticulars")
    @Operation(summary = "随机获取相关论坛")
    public CommonResult<List<FamForumRespTwoVO>> getFamForumRandomParticulars(@Valid @RequestBody FamForumPageReqVO pageVO) {
        List<FamForumRespTwoVO> pageResult = famForumService.getFamForumRandomParticulars(pageVO);
        return success(pageResult);
    }


    @GetMapping("/InformPage")
    @Operation(summary = "获得谈论点赞/回复通知")
    public CommonResult<IPage<FamForumiIformRespVO>> getFamForumInformPage(@Valid FamForumInformPageReqVO pageVO) {
        IPage<FamForumiIformRespVO> pageResult = famForumService.getFamForumInformPage(pageVO);
        return success(pageResult);
    }

    @GetMapping("/Informread")
    @Operation(summary = "点赞/收藏 谈论回复 通知已读")
    public CommonResult<Boolean> getFamForumInformread(@Valid List<FamForumiIformRespVO> pageVO) {
        famForumService.getFamForumInformread(pageVO);
        return success(true);
    }

}
